package com.linln.modules.ad.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.linln.common.constant.NoticeTypeConst;
import com.linln.common.constant.ShenheStatusConst;
import com.linln.common.data.PageSort;
import com.linln.common.enums.StatusEnum;
import com.linln.modules.ad.domain.AdInfo;
import com.linln.modules.ad.repository.AdInfoRepository;
import com.linln.modules.ad.service.AdInfoService;
import com.linln.modules.member.domain.Membernotice;
import com.linln.modules.member.service.MembernoticeService;
import com.linln.modules.member.service.NoticeService;

/**
 * @author hct
 * @date 2019/09/12
 */
@Service
public class AdInfoServiceImpl implements AdInfoService {

    @Autowired
    private AdInfoRepository adInfoRepository;
   
    @Autowired
    private MembernoticeService membernoticeService;
    @Autowired
    private NoticeService noticeService;

    /**
     * 根据ID查询数据
     * @param id 主键ID
     */
    @Override
    @Transactional
    public AdInfo getById(Long id) {
        return adInfoRepository.findById(id).orElse(null);
    }

    /**
     * 获取分页列表数据
     * @param example 查询实例
     * @return 返回分页数据
     */
    @Override
    public Page<AdInfo> getPageList(Example<AdInfo> example) {
        // 创建分页对象
        PageRequest page = PageSort.pageRequest();
        return adInfoRepository.findAll(example, page);
    }
    
    @Override
    public Page<AdInfo> getPageList(Example<AdInfo> example,Integer pageIndex,Integer pageSize){
    	 // 创建分页对象
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        if(pageIndex==null) {
        	pageIndex=1;
        }
        
    	PageRequest page = PageSort.pageRequest(pageIndex,pageSize,sort);
        return adInfoRepository.findAll(example, page);
    }

    /**
     * 获取列表数据
     * @param example 查询实例
     * @param sort 排序对象
     * @return 列表
     */
   public  List<AdInfo> getListByExample(Example<AdInfo> example, Sort sort){
    	return adInfoRepository.findAll(example, sort);
    }

    /**
     * 保存数据
     * @param adInfo 实体对象
     */
    @Override
    public AdInfo save(AdInfo adInfo) {
        return adInfoRepository.save(adInfo);
    }
    @Override
    public AdInfo  saveAndSendNotice(AdInfo adInfo) {
    	AdInfo baseinfonew=adInfoRepository.save(adInfo);
    	/*if(baseinfonew!=null && baseinfonew.getId()>0) {
    		//发送消息
    		membernoticeService.updateIsread(3,baseinfonew.getId());
     		//ok发送消息
    		Notice notice=new Notice();
    		notice.setArea(adInfo.getArea());
    		notice.setContent("一条待审核信息");
            notice.setCreateDate(new Date());
            notice.setMember(adInfo.getMember());
            notice.setType(NoticeTypeConst.MESSAGE);
            notice.setTypeId(baseinfonew.getId());
            notice.setIsread(0); 
    		noticeService.save(notice);
     	}*/
    	return baseinfonew;
    }

    
    /**
     * 广告并生成订单数据
     * @param adInfo 实体对象
     */
    public AdInfo saveAd(AdInfo adInfo) {
    	   AdInfo ad=adInfoRepository.save(adInfo);
     	   return ad;
    }

    /**
     * 状态(启用，冻结，删除)/批量状态处理
     */
    @Override
    @Transactional
    public Boolean updateStatus(StatusEnum statusEnum, List<Long> idList) {
        return adInfoRepository.updateStatus(statusEnum.getCode(), idList) > 0;
    }
    @Override
    @Transactional
   public Boolean updateAdStatus(Integer adStatus,Long id) {
    	AdInfo adInfo=adInfoRepository.getOne(id);
		
    	if(adInfoRepository.updateAdStatus(adStatus,id)>0) {
    		//ok发送消息
    		Membernotice membernotice=new Membernotice();
    		membernotice.setArea(adInfo.getArea());  
    		membernotice.setTypeid(id);
    		if(adStatus==ShenheStatusConst.OK) {
    			//TODO 将钱累加到代理商账号下
    			//记录一个订单并将钱累加到当前区域的代理商账号下。
    			//memberAgentMoneyService
    			membernotice.setContent("审核信息通过");
        		
    		}else if(adStatus==ShenheStatusConst.REFUSE) {
    			//TODO将钱退还给用户
    			membernotice.setContent("审核信息拒绝");

    		}
    		membernotice.setIsread(0); 
    		membernotice.setCreateDate(new Date());
    		membernotice.setMember(adInfo.getMember());
    		membernotice.setNoticestatus(adStatus);
    		membernotice.setType(NoticeTypeConst.AD);
    		membernoticeService.save(membernotice);
    		return true;
    	}else {
    		return false;
    	}
    }
    
    /**
     * 信息的阅读数增加1
     */
    @Override
    @Transactional
    public Boolean updateIsReadAddById(Long id) {
    	return adInfoRepository.updateIsReadAddById(id) > 0;
    }
    
	public List<AdInfo> queryListByTypeAndArea(Integer adtypeid,String area){
		 return adInfoRepository.queryListByTypeAndArea(adtypeid, area);
	}
	

	public List<AdInfo> queryListByType(Integer adtypeid){
		 return adInfoRepository.queryListByType(adtypeid);

	}

	public  AdInfo findByOrderId(String orderId) {
		return adInfoRepository.findByOrderId(orderId);
	}
	
    public Integer updatePayStatus( String orderId) {
    	return adInfoRepository.updatePayStatus(orderId);
    }

    public Integer updateRefundStatus( String orderId) {
    	return adInfoRepository.updateRefundStatus(orderId);
    }


}